//============================================================================
// Name        : simple_factory.cpp
// Author      : Tony
// Version     :
// Copyright   : GPLv2
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
using namespace std;

class abstractProduct{
public:
	abstractProduct()
	{
		cout<<"abstractProduct created!"<<endl;
	}
	virtual ~abstractProduct(){
		cout<<"abstractProduct destroyed!"<<endl;
	}
	virtual void use() = 0;
};

class P1 : public abstractProduct {
public:
	P1()
	{
		cout<<"P1 created!"<<endl;
	}
	~P1() {
		cout<<"P1 destroyed!"<<endl;
	}
	void use(){
		cout<<"use P1"<<endl;
	}
};

class P2 : public abstractProduct
{
public:
	P2()
	{
		cout<<"P2 created!"<<endl;
	}
	~P2() {
		cout<<"P2 destroyed!"<<endl;
	}
	void use(){
		cout<<"use P2"<<endl;
	}
};
typedef enum {
	PRODUCT1,
	PRODUCT2
} productType;

class factory{
public:
	static abstractProduct* createProduct(productType product )
	{
		switch(product){
		case PRODUCT1:
			return new P1();
		case PRODUCT2:
			return new P2();
		default:
			return NULL;
		}
	}
};

int main() {
	abstractProduct* p = factory::createProduct(PRODUCT1);
	p->use();
	delete p;
	cout <<"================"<<endl;
	p = factory::createProduct(PRODUCT2);
	p->use();
	delete p;
	return 0;
}
